package com.goddess.yeecolor.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.goddess.yeecolor.Modle.basic;
import com.goddess.yeecolor.Modle.belong_info;
import com.goddess.yeecolor.Modle.comments;
import com.goddess.yeecolor.Modle.user_info;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.activity.MyAppClient;
import com.goddess.yeecolor.tools.ImageUtils;
import com.goddess.yeecolor.view.CircleImageView;

import java.util.List;

public class item_Exerciseitem_commit_contentAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ViewHolder viewHolder;
	private OnClickListener replyToReplyListener;
	private int parentPosition = -1;
	private List<comments>replyList;
	private boolean isshow;//头像是否显示
	public enum TYPE{
		TYPE_ONE,TYPE_TWO
	}
	public item_Exerciseitem_commit_contentAdapter(Context context, List<comments> replyList,
												   int parentPosition, OnClickListener replyToReplyListener,boolean isshow) {
		this.inflater = LayoutInflater.from(context);
		this.parentPosition = parentPosition;
		this.replyToReplyListener = replyToReplyListener;
		this.replyList = replyList;
		this.isshow = isshow;
	}

	@Override
	public int getCount() {
		return replyList.size();
	}

	public void clearList() {
		this.replyList.clear();
	}

	public void updateList(List<comments> replyList) {
		this.replyList.addAll(replyList);
	}

	@Override
	public comments getItem(int position) {
		return replyList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			viewHolder = new ViewHolder();
			if (isshow){
				convertView = inflater.inflate(R.layout.item_comment_reply2, null);
				viewHolder.iv_user_photo= (CircleImageView) convertView.findViewById(R.id.iv_user_photo);
				viewHolder.tv_comment_reply_time= (TextView) convertView.findViewById(R.id.tv_comment_reply_time);
			}
			else {
				convertView = inflater.inflate(R.layout.item_comment_reply, null);
			}
			viewHolder.tv_comment_reply_text = (TextView) convertView
					.findViewById(R.id.tv_comment_reply_text);
			viewHolder.tv_comment_reply_writer = (TextView) convertView
					.findViewById(R.id.tv_comment_reply_writer);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		comments reply = getItem(position);

		user_info user=reply.getInfo();
		basic basic=user.getBasic();
		belong_info belong_info=reply.getBelong();
		basic basicb=belong_info.getBasic();
		if (isshow){
			ImageUtils.getInstance().ImageStart(MyAppClient.getInstance(),basic.getThumb(),viewHolder.iv_user_photo);
			viewHolder.tv_comment_reply_time.setText(reply.getTime()+"");
		}
		if (isshow){
//			viewHolder.tv_comment_reply_text.setText(reply.getContent());
			// 判断是回复楼主或者回复其他人
			if (basic.getId()==basicb.getId()) {
				viewHolder.tv_comment_reply_writer.setText(basic.getNickname());
				viewHolder.tv_comment_reply_text.setText(reply.getContent());
			} else {
				int start = basic.getNickname().length();
				String text = "回复" + basicb.getNickname()+": "+reply.getContent();
				int length=reply.getContent().length();
				SpannableStringBuilder ss = new SpannableStringBuilder(text);
				// 设置指定位置文字的背景颜色（将“回复”设置成灰色）
				ss.setSpan(new ForegroundColorSpan(Color.parseColor("#7E2E73")),//获取长度改变字体颜色  有个问题显示的长度只有获取到的一半
						2, length+3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
				viewHolder.tv_comment_reply_writer.setText(basic.getNickname());
				viewHolder.tv_comment_reply_text.setText(ss);
			}
		}
		else {
			viewHolder.tv_comment_reply_text.setText(reply.getContent());
			// 判断是回复楼主或者回复其他人
			if (basic.getId()==basicb.getId()) {
//				LogUtils.getInstence().info("评论有没有-----"+basic.getNickname());
				viewHolder.tv_comment_reply_writer.setText(basic.getNickname()+" :");
			} else {

				int start = basic.getNickname().length();
				String text = basic.getNickname() + "回复: " +basicb.getNickname();
				SpannableStringBuilder ss = new SpannableStringBuilder(text);
				// 设置指定位置文字的背景颜色（将“回复”设置成灰色）
				ss.setSpan(new ForegroundColorSpan(Color.parseColor("#7E2E73")),
						start, start + 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
				viewHolder.tv_comment_reply_writer.setText(ss);
			}
		}
		// 记录发表回复时，对应的position,用于确定所回复的对象，如果是楼中楼，还需记录父节点的position
		convertView.setTag(R.id.tag_first, parentPosition);
		convertView.setTag(R.id.tag_second, position);
		convertView.setOnClickListener(replyToReplyListener);
		return convertView;
	}

	public class ViewHolder {
		private TextView tv_comment_reply_writer; // 评论者昵称
		private TextView tv_comment_reply_text; // 评论 内容
		private CircleImageView iv_user_photo;
		private TextView tv_comment_reply_time;
	}

	/**
	   * 删除一条
	   * @param index id号
	   */

	public void addDate(int position,comments comments){
		replyList.add(position,comments);
		notifyDataSetChanged();
	}

}
